import { _decorator, Component, Label, Node, Size, Sprite, UITransform, Vec3, Animation } from 'cc';
import { AppControll, EventName, PopView, prop_config, secretShop_config, skin_config } from '../manager/AppControll';
import { DataHelper } from '../system/DataHelper';
import { Loader } from '../tools/LoadManger';
import { Tools } from '../tools/Tools';
import { GameData } from '../system/GameData';
import EvtManger from '../tools/EvtManger';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('SecretShopItem')
export class SecretShopItem extends Component {
    @property(Node)
    mNdFreeBtn: Node = null;
    @property(Node)
    mNdAdBtn: Node = null;

    private mSPGoodSprite: Sprite = null;
    private mLbDescription: Label = null;
    private mReward: skin_config = null;
    private mConfig: secretShop_config = null;
    private mNdMultiple: Node = null;
    protected onLoad(): void {
        this.mNdFreeBtn.on(Node.EventType.TOUCH_END, this.OnBtnComfirn, this);
        this.mNdAdBtn.on(Node.EventType.TOUCH_END, this.OnBtnAdComfirn, this);
        this.mNdMultiple = this.node.getChildByName("multiple");
        this.RegisterEvents();
    }

    RegisterEvents() {
        EvtManger.on(EventName.updateShop, this, this.InitButton);
    }

    protected onDestroy(): void {
        EvtManger.off(EventName.updateShop, this, this.InitButton);
    }

    OnItemRefresh() {

    }

    InitButton() {
        if (GameData.playerData.secretShopFreeTimes > 0) {
            this.mNdAdBtn.active = false;
            this.mNdFreeBtn.active = true;
        } else if (GameData.playerData.secretShopFreeTimes <= 0 && GameData.playerData.secretShopVideoTimes > 0) {
            this.mNdAdBtn.active = true;
            this.mNdAdBtn.getComponent(Sprite).grayscale = false;
            this.mNdFreeBtn.active = false;
        } else if (GameData.playerData.secretShopFreeTimes <= 0 && GameData.playerData.secretShopVideoTimes <= 0) {
            this.mNdAdBtn.active = true;
            this.mNdAdBtn.getComponent(Sprite).grayscale = true;
            this.mNdFreeBtn.active = false;
        }
    }

    InitItem(config: secretShop_config) {
        this.mConfig = config;
        this.node.position = new Vec3(this.node.position.x, 0);
        this.mSPGoodSprite = this.node.getChildByName("reward_month_icon_gold").getComponent(Sprite);
        this.mLbDescription = this.node.getChildByName("Label").getComponent(Label);
        let propConfig = DataHelper.GetPropConfigById(config.rewardProp);
        let size = new Size(propConfig.IconSize[0], propConfig.IconSize[1]);
        if (config.LabelMultiple) {
            this.mNdMultiple.active = true;
            this.mNdMultiple.getComponent(Animation).play();
            this.mNdMultiple.getChildByName("num").getComponent(Label).string = config.LabelMultiple + "";
        } else {
            this.mNdMultiple.active = false;
        }

        if (propConfig.propType == 2) {
            let skinConfig = DataHelper.GetSkin_config();
            const keys = Object.keys(skinConfig);
            let randomIndex = Math.floor(Math.random() * keys.length);
            this.mReward = skinConfig[keys[randomIndex]];
            Loader.LoadGunImage("mainGunSkin/" + this.mReward.mainGunSkinRes, (spf) => {
                if (!this && !this.mSPGoodSprite) return;
                this.mSPGoodSprite.spriteFrame = spf;
                this.mSPGoodSprite.node.getComponent(UITransform).setContentSize(size);
            });
        } else {
            Loader.GetTurnTableRewardImage("rewardIcon/" + propConfig.propIcon, (spf) => {
                if (!this && !this.mSPGoodSprite) return;
                this.mSPGoodSprite.spriteFrame = spf;
                this.mSPGoodSprite.node.getComponent(UITransform).setContentSize(size);
            })
        }
        this.mLbDescription.string = this.GetPropStr(propConfig);
        this.InitButton();
    }

    GetPropStr(propConfig: prop_config) {
        if (!DataHelper.GetMonsterRefreshByAuto().LimitedTimeAwardGold) return;
        let str = ""
        if (propConfig.propType == 1) {
            let count = propConfig.propNum * DataHelper.GetMonsterRefreshByAuto().LimitedTimeAwardGold;
            str = propConfig.propName + "*" + Tools.FormatNumber(count);
        } else if (propConfig.propType == 2) {
            if (this.mReward) {
                str = this.mReward.skinName + "*" + propConfig.propNum
            }
        }
        else {
            str = propConfig.propName + "*" + propConfig.propNum;
        }
        return str;
    }

    OnBtnComfirn() {
        if (!this.mConfig) return;
        AppControll.Platform.SendBuryPoint(100017);
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (GameData.playerData.secretShopFreeTimes <= 0) return;
        GameData.playerData.secretShopFreeTimes--;
        this.GetRward()
        this.mSPGoodSprite.spriteFrame = null;
        this.node.active = false;
    }

    OnBtnAdComfirn() {
        if (!this.mConfig) return;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        AppControll.Platform.ShowRewardVideoAd(this, (bool, str) => {
            if (bool) {
                AppControll.Platform.SendBuryPoint(100016);
                if (GameData.playerData.secretShopVideoTimes <= 0) {
                    PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, nd => {
                        nd.getComponent(SystemMessage).InitContent("今日次数已用完，请明日再来！")
                    })
                    return;
                }
                GameData.playerData.secretShopVideoTimes--;
                if (this.node) this.node.active = false;
                this.GetRward()
            }
        })
    }

    GetRward() {
        if (!this.mConfig) return;
        let porpConfig = DataHelper.GetPropConfigById(this.mConfig.rewardProp);;
        let ref = DataHelper.GetMonsterRefreshByAuto();
        switch (porpConfig.propType) {
            case 1:
                GameData.AddGold(porpConfig.propNum * ref.LimitedTimeAwardGold);
                EvtManger.do(EventName.dropGiftGold, this.node.position.add(this.node.parent.position));
                break;
            case 2:
                EvtManger.do(EventName.dropSkin, this.mReward, porpConfig.propNum, this.node.position.add(this.node.parent.position));
                break;
            case 3:
                GameData.playerData.blueStuff += porpConfig.propNum
                EvtManger.do(EventName.dropMaterial, porpConfig, this.node.position.add(this.node.parent.position));
                break;
            case 4:
                GameData.playerData.purpleStuff += porpConfig.propNum
                EvtManger.do(EventName.dropMaterial, porpConfig, this.node.position.add(this.node.parent.position));
                break;
            case 5:
                GameData.playerData.yellowStuff += porpConfig.propNum
                EvtManger.do(EventName.dropMaterial, porpConfig, this.node.position.add(this.node.parent.position));
                break;
            case 6:
                GameData.playerData.redStuff += porpConfig.propNum
                EvtManger.do(EventName.dropMaterial, porpConfig, this.node.position.add(this.node.parent.position));
                break;
            case 7:
                EvtManger.do(EventName.dropPearls, porpConfig.propNum, this.node.position.add(this.node.parent.position));
                break;

        }
        Tools.RemoveElementByVaule(GameData.playerData.secretShopReward, this.mConfig.shopId);
        EvtManger.do(EventName.updateShop);
        GameData.SavePlayData();
    }
}


